package be.phl.layoutAdapter;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.devsmart.android.ui.HorizontalListView;

import android.app.Activity;
import android.database.Cursor;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import be.phl.model.DBAdapterScreening;
import be.phl.obstacleapp.R;

public class ItemProblemColumnAdapter extends BaseAdapter {
	/**
	 * TODO: Colors, Values
	 */

	private static final String TAG = "ItemProblemColumnAdapter";

	// list with all the itemproblems for this item
	private List<ItemProblemRow> datalist;
	private List<ItemProblemRowAdapter> lists = new ArrayList<ItemProblemRowAdapter>();
	private Activity activity;

	public ItemProblemColumnAdapter(Activity activity, int ScreeningItemId) {
		super();
		this.activity = activity;

		datalist = new ArrayList<ItemProblemRow>();

		// get all the possible problems from the database
		DBAdapterScreening db = new DBAdapterScreening(activity);
		db.open();
		Cursor c = db.getAllItemProblems(ScreeningItemId);
		while (c.moveToNext()) {
			ItemProblemRow ip = new ItemProblemRow(c.getInt(0),
					ScreeningItemId, c.getInt(2), c.getString(3), c
							.getString(4).equals("T"), c.getInt(7) > 0, c
							.getString(5).equals("T"));
			if (ip.isChecked())
				ip.setScreeningProblemId(c.getInt(7));
			datalist.add(ip);
		}
		db.close();

		// get a list of all the parentId's
		List<Integer> tempParent = new ArrayList<Integer>();
		for (ItemProblemRow i : datalist) {
			if (!tempParent.contains(i.getParentId()) && i.getParentId() >= 0) {
				tempParent.add(i.getParentId());
			}
		}

		// check each item if it is another items parentId
		// if it is not, it is the and of a branch
		for (ItemProblemRow i : datalist) {
			if (!tempParent.contains(i.getId())) {
				i.setBranchEnd(true);
			}
		}

		fillTreeView(-1);
	}

	/**
	 * 
	 * Fill the content of the treeview add a row based on the id from the item
	 * clicked
	 * 
	 * @param ParentId
	 */
	public void fillTreeView(int problemId) {

		// temporary list to collect the data for the new column
		List<ItemProblemRow> tempList = new ArrayList<ItemProblemRow>();
		for (ItemProblemRow i : datalist) {
			if (i.getParentId() == problemId) {
				tempList.add(i);
			}
		}

		// add the temporary list to the adapter
		lists.add(new ItemProblemRowAdapter(this.activity, tempList));
	}

	@Override
	public int getCount() {
		return lists.size();
	}

	@Override
	public Object getItem(int position) {
		return lists.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = activity.getLayoutInflater();
		View columnView = inflater.inflate(R.layout.item_problem_column,
				parent, false);

		final HorizontalListView parentListview = (HorizontalListView) parent;

		ListView listview = (ListView) columnView.findViewById(R.id.list);
		listview.setAdapter(lists.get(position));

		final int posX = position;
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int posY,
					long id) {

				// if the clicked item is not in the last column, delete the
				// column's after clicked column
				int temp = lists.size() - 1;
				Log.i(TAG, "Temp: " + temp);
				while (posX < (lists.size() - 1)) {

					// set the selected of the items false wich are in the child
					// node of the selected item
					for (int i = 0; i < ((ItemProblemRowAdapter) parentListview
							.getAdapter().getItem(temp)).getCount(); i++) {
						ItemProblemRow tempRow = ((ItemProblemRowAdapter) parentListview
								.getAdapter().getItem(temp)).getItem(i);
						if (!tempRow.isBranchEnd())
							tempRow.setSelected(false);
					}
					temp--;

					lists.remove(lists.size() - 1);
					// parentListview.scrollBy(-100, 0);
				}

				ItemProblemRow problem = (ItemProblemRow) parent.getAdapter()
						.getItem(posY);

				// check if the item is at the end of a branch
				if (!problem.isBranchEnd()) {
					fillTreeView(problem.getId());

					Log.i(TAG, "Position: " + parentListview.getX());
				}

				ItemProblemColumnAdapter.this.notifyDataSetChanged();

				// update the selected to update which branch is selected
				for (int i = 0; i < parent.getAdapter().getCount(); i++) {
					((ItemProblemRow) parent.getAdapter().getItem(i))
							.setSelected(false);
				}
				((ItemProblemRow) parent.getAdapter().getItem(posY))
						.setSelected(true);
			}
		});

		return columnView;
	}
}
